﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class TaskCreationMasterDO
    {
        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static TaskCreationMasterDO GetModelByPK(Session sess, int iIntMasterNum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel + " WHERE INTERNAL_MASTER_NUM = @INTERNAL_MASTER_NUM "; ;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_MASTER_NUM", iIntMasterNum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据
        /// </summary>
        /// <remarks></remarks>
        public int Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "CFG_ITaskCreationMaster";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM TASK_CREATION_MASTER WHERE INTERNAL_MASTER_NUM = @INTERNAL_MASTER_NUM";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "CFG_UTaskCreationMaster";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象ALLOCATIONRULEDETAIL的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<TaskCreationMasterDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static DataTable GetAllToTable(Session sess)
        {
            DataHelper helper = null;
            string stSQL = SelectSequel;
            try
            {
                helper = new DataHelper(sess);
                return helper.GetTable(CommandType.Text, stSQL);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性ALLOCATIONRULEDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<TaskCreationMasterDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 从DataView装载持久性ALLOCATIONRULEDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<TaskCreationMasterDO> LoadListFromDataView(DataView dv)
        {
            List<TaskCreationMasterDO> list = new List<TaskCreationMasterDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }
        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static TaskCreationMasterDO GetModel(DataRow row)
        {
            TaskCreationMasterDO obj = new TaskCreationMasterDO();
            if (!row["INTERNAL_MASTER_NUM"].Equals(DBNull.Value)) obj.INTERNAL_MASTER_NUM = Convert.ToInt32(row["INTERNAL_MASTER_NUM"]);
            if (!row["RECORD_TYPE"].Equals(DBNull.Value)) obj.RECORD_TYPE = Convert.ToString(row["RECORD_TYPE"]);
            if (!row["DESCRIPTION"].Equals(DBNull.Value)) obj.DESCRIPTION = Convert.ToString(row["DESCRIPTION"]);
            if (!row["TASK_TYPE"].Equals(DBNull.Value)) obj.TASK_TYPE = Convert.ToString(row["TASK_TYPE"]);
            if (!row["PRIORITY"].Equals(DBNull.Value)) obj.PRIORITY = Convert.ToInt32(row["PRIORITY"]);
            if (!row["FILTER_NAME"].Equals(DBNull.Value)) obj.FILTER_NAME = Convert.ToString(row["FILTER_NAME"]);
            if (!row["MAXIMUM_QTY"].Equals(DBNull.Value)) obj.MAXIMUM_QTY = Convert.ToInt32(row["MAXIMUM_QTY"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) obj.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["MAXIMUM_WEIGHT"].Equals(DBNull.Value)) obj.MAXIMUM_WEIGHT = Convert.ToDouble(row["MAXIMUM_WEIGHT"]);
            if (!row["WEIGHT_UM"].Equals(DBNull.Value)) obj.WEIGHT_UM = Convert.ToString(row["WEIGHT_UM"]);
            if (!row["MAXIMUM_VOLUME"].Equals(DBNull.Value)) obj.MAXIMUM_VOLUME = Convert.ToDouble(row["MAXIMUM_VOLUME"]);
            if (!row["VOLUME_UM"].Equals(DBNull.Value)) obj.VOLUME_UM = Convert.ToString(row["VOLUME_UM"]);
            if (!row["MAXIMUM_INSTRUCTIONS"].Equals(DBNull.Value)) obj.MAXIMUM_INSTRUCTIONS = Convert.ToInt32(row["MAXIMUM_INSTRUCTIONS"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) obj.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) obj.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["TASK_ID_FIELD"].Equals(DBNull.Value)) obj.TASK_ID_FIELD = Convert.ToString(row["TASK_ID_FIELD"]);
            if (!row["INTERNAL_TASK_TYPE"].Equals(DBNull.Value)) obj.INTERNAL_TASK_TYPE = Convert.ToString(row["INTERNAL_TASK_TYPE"]);
            return obj;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [TASK_CREATION_MASTER] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(TaskCreationMasterDO obj)
        {
            SqlParameter[] paras = new SqlParameter[17];
            paras[0] = new SqlParameter("@INTERNAL_MASTER_NUM", obj.INTERNAL_MASTER_NUM);
            paras[0].DbType = DbType.String;
            paras[1] = new SqlParameter("@DESCRIPTION", obj.DESCRIPTION == String.Empty ? DBNull.Value : (object)obj.DESCRIPTION);
            paras[1].DbType = DbType.String;
            paras[2] = new SqlParameter("@TASK_TYPE", obj.TASK_TYPE == String.Empty ? DBNull.Value : (object)obj.TASK_TYPE);
            paras[2].DbType = DbType.String;
            paras[3] = new SqlParameter("@PRIORITY", obj.PRIORITY);
            paras[3].DbType = DbType.String;
            paras[4] = new SqlParameter("@FILTER_NAME", obj.FILTER_NAME == String.Empty ? DBNull.Value : (object)obj.FILTER_NAME);
            paras[4].DbType = DbType.String;
            paras[5] = new SqlParameter("@MAXIMUM_QTY", obj.MAXIMUM_QTY);
            paras[5].DbType = DbType.String;
            paras[6] = new SqlParameter("@QUANTITY_UM", obj.QUANTITY_UM == String.Empty ? DBNull.Value : (object)obj.QUANTITY_UM);
            paras[6].DbType = DbType.String;
            paras[7] = new SqlParameter("@MAXIMUM_WEIGHT", obj.MAXIMUM_WEIGHT);
            paras[7].DbType = DbType.String;
            paras[8] = new SqlParameter("@WEIGHT_UM", obj.WEIGHT_UM == String.Empty ? DBNull.Value : (object)obj.WEIGHT_UM);
            paras[8].DbType = DbType.String;
            paras[9] = new SqlParameter("@MAXIMUM_VOLUME", obj.MAXIMUM_VOLUME);
            paras[9].DbType = DbType.String;
            paras[10] = new SqlParameter("@VOLUME_UM", obj.VOLUME_UM == String.Empty ? DBNull.Value : (object)obj.VOLUME_UM);
            paras[10].DbType = DbType.String;
            paras[11] = new SqlParameter("@MAXIMUM_INSTRUCTIONS", obj.MAXIMUM_INSTRUCTIONS);
            paras[11].DbType = DbType.String;
            paras[12] = new SqlParameter("@USER_STAMP", obj.USER_STAMP == String.Empty ? DBNull.Value : (object)obj.USER_STAMP);
            paras[12].DbType = DbType.String;
            paras[13] = new SqlParameter("@DATE_TIME_STAMP", System.DateTime.Now);
            paras[13].DbType = DbType.DateTime;
            paras[14] = new SqlParameter("@TASK_ID_FIELD", obj.TASK_ID_FIELD == String.Empty ? DBNull.Value : (object)obj.TASK_ID_FIELD);
            paras[14].DbType = DbType.String;
            paras[15] = new SqlParameter("@RECORD_TYPE", obj.RECORD_TYPE == String.Empty ? DBNull.Value : (object)obj.RECORD_TYPE);
            paras[15].DbType = DbType.String;
            paras[16] = new SqlParameter("@INTERNAL_TASK_TYPE", obj.INTERNAL_TASK_TYPE == String.Empty ? DBNull.Value : (object)obj.INTERNAL_TASK_TYPE);
            paras[16].DbType = DbType.String;


            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(TaskCreationMasterDO obj)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@OBJECT_ID", obj.INTERNAL_MASTER_NUM);

            return paras;
        }

        #endregion

        public static List<TaskCreationMasterDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
